<?php


namespace app\shop\controller\api;


use app\BaseController;
use app\enterprise\model\User;
use app\shop\model\file\FileModel;
use app\shop\model\user\CollectImgModel;
use app\shop\service\until\FileService;
use app\shop\service\user\UserCollectService;
use think\facade\Request;

class UploadController extends BaseController
{
    /**
     * 聊天文件上传
     * @param FileService $fileService
     * @return \think\response\Json
     */
    public function uploadFile(FileService $fileService)
    {
        try {
            $userId = $this->request->middleware('userId');

            $file = $this->request->file('file');

            $params = $this->request->param();

            $info = $fileService->upload($userId, $params, $file);

            return $this->success($info);
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 上传头像
     * @param FileService $fileService
     * @return \think\response\Json
     */
    public function uploadAvatar(FileService $fileService)
    {
        try {
            $file = $this->request->file('file');

            $userId = $this->request->middleware('userId');
            $userInfo = $this->request->middleware('userInfo');

            $info = $fileService->upload($userId, [], $file, 'avatar/' . $userId . '/');

            $userInfo->avatar = $info['src'];
            $userInfo->save();

            $url = getDiskUrl() . '/' . $info['src'];

            return $this->success($url, "上传成功");
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 上传文件
     * @param FileService $fileService
     * @return \think\response\Json
     */
    public function fileUpload(FileService $fileService)
    {
        try {
            $file = $this->request->file('file');
            $userId = $this->request->middleware('userId');
            $info = $fileService->upload($userId, [], $file);
            $filId = $info['file_id'];
            return $this->success($filId);
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 添加收藏表情图片
     * @param FileService $fileService
     * @param UserCollectService $collectService
     * @return \think\response\Json
     */
    public function collectImg(FileService $fileService, UserCollectService $collectService)
    {
        try {
            $file = $this->request->file('file');
            $fileId = $this->request->param('file_id');

            if (!$file && !$fileId) {
                throw new \Exception('请选择收藏的文件');
            }

            $userId = $this->request->middleware('userId');

            if (!$fileId) {

                $info = $fileService->upload($userId, [], $file, 'collect/' . $userId . '/', true, 1);

                $collectService->collectImg($userId, $info['file_id']);
            } else {
                $info = $fileService->getOneLineById($fileId);
                $fileService->updateById($fileId, ['use_type' => 1]);
                $collectService->collectImg($userId, $fileId);
            }


            $url = FileService::getFullUrl($info['src']);

            return $this->success($url, "上传成功");
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     *获取收藏列表
     * @return \think\response\Json
     */
    public function getCollectImgList()
    {
        try {
            $userId = $this->request->middleware('userId');
            $list = CollectImgModel::where('user_id', $userId)->field('id,user_id,file_id')->order('id', 'desc')->select();

            foreach ($list as $k => &$value) {
                $fileInfo = FileModel::find($value['file_id']);
                if (!$fileInfo) {
                    unset($list[$k]);
                } else {
                    $value['url'] = $fileInfo ? FileService::getFullUrl($fileInfo['src']) : '';
                }
            }
            return $this->success($list);
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     *删除收藏
     * @return \think\response\Json
     */
    public function delCollectImg()
    {
        try {
            $userId = $this->request->middleware('userId');
            $fileId = $this->request->param('file_id');
            $info = CollectImgModel::where('user_id', $userId)->where('file_id', $fileId)->find();
            if (!$info) {
                throw new \Exception('表情不存在');
            }
            $info->delete();
            return $this->success();
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 上传文件
     * @param FileService $fileService
     * @return \think\response\Json
     */
    public function upload(FileService $fileService)
    {
        try {
            $file = $this->request->file('file');
            $userId = $this->request->middleware('userId');
            $info = $fileService->upload($userId, [], $file);
            $info['path'] = $info['src'];
            $info['src'] = FileService::getFullUrl($info['src']);
            return $this->success($info);
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }
}
